package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.TblAdvertAddress;
import pojo.TblManager;
import vo.TblAdvertAddressVo;

import comment.CountMessage;
import comment.HibernateSessionFactory;

public class TblAdvertAddressDao {
	private Transaction tc = null;

	public List<TblAdvertAddressVo> findTblAdvertAddressByPageList(
			TblAdvertAddressVo tblAdvertAddressVo) {
		List<TblAdvertAddressVo> tblAdvertAddressVoList = new ArrayList();
		try {
			String hql = "select new vo.TblAdvertAddressVo(taa.id,taa.advertName,taa.width,taa.height,"
					+ "taa.notes,tdf.value,taa.defPath) from TblAdvertAddress taa,Tbldeftype tdf where taa.defType=tdf.code";
			if (tblAdvertAddressVo.getAdvertName() != null
					&& !(tblAdvertAddressVo.getAdvertName().isEmpty())) {
				hql += " and taa.advertName like '%"
						+ tblAdvertAddressVo.getAdvertName() + "%'";
			}
			if (tblAdvertAddressVo.getAdvertName() == null
					|| !(tblAdvertAddressVo.getAdvertName().isEmpty())) {
				tblAdvertAddressVo.setDefType("1");
			}
			hql += " and taa.defType=" + tblAdvertAddressVo.getDefType();
			Session session = HibernateSessionFactory.getSession();
			Query hqlQuery = session.createQuery(hql);
			hqlQuery.setFirstResult((tblAdvertAddressVo.getCurPage() - 1)
					* tblAdvertAddressVo.getSizePerPage());
			hqlQuery.setMaxResults(tblAdvertAddressVo.getSizePerPage());
			tblAdvertAddressVoList = hqlQuery.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tblAdvertAddressVoList;
	}
	public int findTblAdvertAddressNumList(
			TblAdvertAddressVo tblAdvertAddressVo) {
		int resultCount=0;
		try {
			String hql = "select count(*) from TblAdvertAddress taa,Tbldeftype tdf where taa.defType=tdf.code";
			if(tblAdvertAddressVo.getAdvertName()!=null&&!(tblAdvertAddressVo.getAdvertName().isEmpty())){
				hql += " and taa.advertName like '%"+tblAdvertAddressVo.getAdvertName()+"%'";
			}
			if(tblAdvertAddressVo.getAdvertName()==null||!(tblAdvertAddressVo.getAdvertName().isEmpty())){
				tblAdvertAddressVo.setDefType("1");
			}
			hql += " and taa.defType="+tblAdvertAddressVo.getDefType();
			Session session = HibernateSessionFactory.getSession();
			Query hqlQuery = session.createQuery(hql);
			List resultList = hqlQuery.list();
			resultCount = ((Long)resultList.get(0)).intValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultCount;
	}
	public void insertTblAdvertAddress(TblAdvertAddress tblAdvertAddress) {
		try {
			Session session = HibernateSessionFactory.getSession();
			tc = session.beginTransaction();
			session.save(tblAdvertAddress);
			tc.commit();

		} catch (Exception e) {
			e.printStackTrace();
			if (tc != null) {
				tc.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	public void uptTblAdvertAddress(TblAdvertAddress tblAdvertAddress) {
		try {
			Session session = HibernateSessionFactory.getSession();
			tc = session.beginTransaction();
			session.update(tblAdvertAddress);
			tc.commit();

		} catch (Exception e) {
			e.printStackTrace();
			if (tc != null) {
				tc.rollback();
			}
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public List<TblAdvertAddressVo> findTblAdvertAddressList(
			TblAdvertAddressVo tblAdvertAddressVo) {
		List<TblAdvertAddressVo> tblAdvertAddressVoList = new ArrayList();
		try {
			String hql = "select new vo.TblAdvertAddressVo(taa.id,taa.advertName,taa.width,taa.height,"
					+ "taa.notes,tdf.value,taa.defPath) from TblAdvertAddress taa,Tbldeftype tdf where taa.defType=tdf.code";
			if(tblAdvertAddressVo.getAdvertName()!=null&&!(tblAdvertAddressVo.getAdvertName().isEmpty())){
				hql += " and taa.advertName like '%"+tblAdvertAddressVo.getAdvertName()+"%'";
			}
			if(tblAdvertAddressVo.getAdvertName()==null||!(tblAdvertAddressVo.getAdvertName().isEmpty())){
				tblAdvertAddressVo.setDefType("1");
			}
			hql += " and taa.defType="+tblAdvertAddressVo.getDefType();
			Session session = HibernateSessionFactory.getSession();
			Query hqlQuery = session.createQuery(hql);
			tblAdvertAddressVoList = hqlQuery.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tblAdvertAddressVoList;
	}
	public void delectAdvertAddress(String ids){
		Transaction tc = null;
		try{
			String hql = "delete from TblAdvertAddress where id in ("+ids+")";
			Session session = HibernateSessionFactory.getSession();
			tc = session.beginTransaction();
			Query hqlQuery = session.createQuery(hql);
			hqlQuery.executeUpdate();
			tc.commit();
		}catch(Exception e){
			tc.rollback();
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
	public TblAdvertAddress searchUpdateAdvertAddress(TblAdvertAddressVo tblAdvertAddressVo){
		TblAdvertAddress tblAdvertAddress = new TblAdvertAddress();
		try {
			Session session = HibernateSessionFactory.getSession();
			tblAdvertAddress = (TblAdvertAddress) session.get(
					TblAdvertAddress.class, tblAdvertAddressVo.getId());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return tblAdvertAddress;
	}

}
